package com.wy.Service.impl;

import com.wy.Service.WeixiuService;
import com.wy.dao.ResideDao;
import com.wy.dao.WeixiuDao;
import com.wy.pojo.Reside;
import com.wy.pojo.Weixiu;
import com.wy.vo.PageVo;
import com.wy.vo.WeixiuDto;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service("weixiuService")
@Transactional
public class WeixiuSrviceImpl extends BaseServiceImpl implements WeixiuService {

    @Resource
    private WeixiuDao weixiuDao;

    @Resource
    private ResideDao resideDao;

    @Override
    public PageVo searchAll(Integer current, Integer pageSize) {
        PageRequest pageRequest= PageRequest.of(current-1,pageSize);
        Page page= weixiuDao.findAll(pageRequest);
        return new PageVo(page);
    }

    @Override
    public PageVo searchAll(Integer current, Integer pageSize, WeixiuDto dto) {
        StringBuilder sbd=new StringBuilder("select k from Weixiu k where 1=1 ");
        if(!StringUtils.isEmpty(dto.getName())){
            sbd.append(" and k.name like :name");
            dto.setName("%"+dto.getName()+"%");
        }

        return super.searchPagination(current,pageSize,sbd.toString(),dto);
    }

    @Override
    public List<Reside> searchReside() {
        return resideDao.findAll();
    }

    @Override
    public void del(Integer id) {
        weixiuDao.deleteById(id);
    }

    @Override
    public void add(Weixiu weixiu) {
        weixiuDao.save(weixiu);
    }

    @Override
    public void update(Integer id, String yanshou, String spyj) {
        weixiuDao.updateRecord(id,yanshou,spyj);
    }


}
